import * as Blockly from 'blockly/core';
import * as layui from 'layui';
import BlockTool from '../blockTool';
import copyPic from '../media/copy.png';

const blocks = {};

const { layer } = layui;

const BLOCK_TOOL_MAIN_HUE = 90;

blocks.block_tool_main = {
    init: function () {
        this.appendDummyInput()
            .appendField("代码区显示生成代码")
            .appendField(new Blockly.FieldCheckbox("TRUE"), "SHOW_CODE");
        this.appendDummyInput()
            .appendField("代码区显示块XML")
            .appendField(new Blockly.FieldCheckbox("TRUE"), "SHOW_BLOCKS_XML");
        this.appendDummyInput()
            .appendField("名字:")
            .appendField(new Blockly.FieldTextInput("block_tool_test"), "NAME");
        this.appendStatementInput("BLOCK")
            .setCheck("BLOCK_TOOL_MAIN_BLOCK")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("Block")
            .appendField(new Blockly.FieldImage(copyPic, 18, 18, "*", () => {
                navigator.clipboard.writeText(BlockTool.blockCode)
                    .then(() => {
                        console.log('clipboard：复制成功');
                        layer.msg('Block代码复制成功', { time: 1000 });
                    }).catch((error) => {
                        console.log('clipboard：复制失败', error);
                        layer.msg('Block代码复制失败', { time: 1000 });
                    });
            }));
        this.appendDummyInput()
            .appendField("显示方式:")
            .appendField(new Blockly.FieldDropdown([
                ["自动调整", "AUTO"],
                ["多行显示", "OUT_LINE"],
                ["单行显示", "IN_LINE"]
            ]), "INPUT_TYPE");
        this.appendDummyInput()
            .appendField("连接方式:")
            .appendField(new Blockly.FieldDropdown([
                ["无连接口", "NO"],
                ["↑  连接口", "TOP"],
                ["↓  连接口", "BOTTOM"],
                ["↕  连接口", "V_BOTH"],
                ["←连接口", "LEFT"]
            ]), "CONNECT_TYPE");
        this.appendValueInput("TOOLTIP")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("提示信息:");
        this.appendValueInput("HELP_URL")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("帮助信息:");
        this.appendValueInput("COLOR")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("颜色:");
        this.appendStatementInput("GENERATOR")
            .setCheck("BLOCK_TOOL_MAIN_GENERATOR")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("Generator")
            .appendField(new Blockly.FieldImage(copyPic, 18, 18, "*", () => {
                navigator.clipboard.writeText(BlockTool.generatorCode)
                    .then(() => {
                        console.log('clipboard：复制成功');
                        layer.msg('Generator代码复制成功', { time: 1000 });
                    }).catch((error) => {
                        console.log('clipboard：复制失败', error);
                        layer.msg('Generator代码复制失败', { time: 1000 });
                    });
            }));
        this.appendStatementInput("XML")
            .setCheck("BLOCK_TOOL_MAIN_XML")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("XML")
            .appendField(new Blockly.FieldImage(copyPic, 18, 18, "*", () => {
                navigator.clipboard.writeText(BlockTool.xmlCode)
                    .then(() => {
                        console.log('clipboard：复制成功');
                        layer.msg('XML代码复制成功', { time: 1000 });
                    }).catch((error) => {
                        console.log('clipboard：复制失败', error);
                        layer.msg('XML代码复制失败', { time: 1000 });
                    });
            }));
        this.setInputsInline(false);
        this.setColour(BLOCK_TOOL_MAIN_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

export default blocks;